package cc.taofoo.IO.socket;

import com.sun.org.apache.xpath.internal.operations.String;

import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.ByteBuffer;
import java.nio.channels.Channel;
import java.nio.channels.SocketChannel;
import java.util.concurrent.*;

public class Server extends Thread {
    public ServerSocket serverSocket;

    public Server(){
        try {
            serverSocket = new ServerSocket(12589);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void run() {
        Socket socket;
        while (true) {
            try {
                socket = serverSocket.accept();
                new Task(socket).start();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }


}

class Task extends Thread{

    Socket socket;
    public Task(Socket socket){
        this.socket = socket;
    }

    @Override
    public void run() {
        try {
            OutputStreamWriter osw = new OutputStreamWriter(socket.getOutputStream());
            osw.write("hello world");
            osw.close();
            socket.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}